﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace OoganizerDAL
{

    public partial class OogyDB : System.Data.Linq.DataContext
    {
        public OogyDB() : base(GetConnectionString()) { }

        static string GetConnectionString()
        {
            string oogyDBLocation = Ooganizer.Platform.Env.GetAppdirectory() + @"\OogyDB.sdf";

            if (!File.Exists(oogyDBLocation))
            {
                try
                {
                    // create database            
                    OogyDB oogyData = new OogyDB(oogyDBLocation);

                    oogyData.Log = Console.Out;                    

                    oogyData.CreateDatabase();
                }
                catch (Exception e)
                {                    
                    throw e;
                }
            }

            return oogyDBLocation;
        }

        partial void DeleteWorkspace(Workspace workspace)
        {
            foreach (var workspaceArtifact in workspace.WorkspaceArtifacts)
            {
                DeleteWorkspaceArtifact(workspaceArtifact);
            }
            
            ExecuteDynamicDelete(workspace);
        }

        partial void DeleteWorkspaceArtifact(WorkspaceArtifact workspaceArtifact)
        {
            Artifact artifact = workspaceArtifact.Artifact;

            // NOTE: Had to do this with explicit SQL to possible bug in LINQ to SQL when generating key for deletion
            ExecuteCommand("delete WorkspaceArtifact where Workspace_id = {0} and Artifact_id = {1}",
                           workspaceArtifact.Workspace_id,
                           workspaceArtifact.Artifact_id);

            if (artifact.WorkspaceArtifacts.Count == 0)
            {
                // this artifact is only referenced by this workspace artifact. It
                // will be orphaned ... delete it as well
                ExecuteDynamicDelete(artifact);
            }
        }
    }

    public partial class Workspace
    {
        partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            switch (action)
            {
                case ChangeAction.Insert:
                case ChangeAction.Update:
                    {
                        OoganizerDAL.OogyDB db = new OoganizerDAL.OogyDB();

                        var workspaces =
                           from w in db.Workspaces
                           select w;

                        foreach (OoganizerDAL.Workspace wsDal in workspaces)
                        {
                            // Make sure that the name is not already present
                            if (Name == wsDal.Name)
                            {
                                throw new System.Data.Linq.DuplicateKeyException(wsDal);
                            }
                        }

                        break;
                    }
                case ChangeAction.Delete:
                default:
                    break;
            }
        }
    }
}
